package cn.bitkit.component.context;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.bitkit.base.util.StringUtil;

/**
 * 
 * @author changbo
 *
 */
public class HttpContext {
	
	public static ServletRequestAttributes getContext() {
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes());
	}
	
	public static HttpServletRequest getRequest() {
		return getContext().getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		return getContext().getResponse();
	}
	
	public static HttpSession getHttpSession() {
		return getRequest().getSession();
	}

	public static void setSession(String key, Object value) {
		getHttpSession().setAttribute(key, value);
	}

	public static void removeSession(String key) {
		getHttpSession().removeAttribute(key);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getSession(String key) {
		return (T) getHttpSession().getAttribute(key);
	}
	
	public static boolean sessionExist(String key) {
		return StringUtil.hasValue(getSession(key));
	}
}
